package com.zkthink.ceres.app.authority.config;

import com.zkthink.boot.config.BaseConfig;
import com.zkthink.ceres.app.interceptor.TokenHandlerInterceptor;
import com.zkthink.ceres.common.properties.IgnoreTokenProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author ceres
 * @createTime 2017-12-15 14:42
 */
@Configuration
@EnableConfigurationProperties({IgnoreTokenProperties.class})
public class AuthorityWebConfiguration extends BaseConfig implements WebMvcConfigurer {

    @Autowired
    private IgnoreTokenProperties ignoreTokenProperties;

    @Bean
    public HandlerInterceptor getTokenHandlerInterceptor() {
        return new TokenHandlerInterceptor(ignoreTokenProperties);
    }

    /**
     * 注册 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] commonPathPatterns = getExcludeCommonPathPatterns();
        registry.addInterceptor(getTokenHandlerInterceptor())
                .addPathPatterns("/**")
                .order(5)
                .excludePathPatterns(commonPathPatterns);
        WebMvcConfigurer.super.addInterceptors(registry);
    }

    /**
     * auth-client 中的拦截器需要排除拦截的地址
     */
    protected String[] getExcludeCommonPathPatterns() {
        String[] urls = {
                "/*.css",
                "/*.js",
                "/*.html",
                "/error",
                "/login",
                "/v2/api-docs",
                "/v2/api-docs-ext",
                "/swagger-resources/**",
                "/webjars/**",

                "/",
                "/csrf",

                "/META-INF/resources/**",
                "/resources/**",
                "/static/**",
                "/public/**",
                "classpath:/META-INF/resources/**",
                "classpath:/resources/**",
                "classpath:/static/**",
                "classpath:/public/**",

                "/cache/**",
                "/swagger-ui.html**",
                "/doc.html**",
                "/auth/wx/miniApp",
                "/auth/wx/miniApp/bindPhone",
                "/order/payOrderCallback",
                "/product/queryProductDetail/**",
                "/cms/**",

                "/pay/notify/order",
                "/pay/notify/refund",
                "/pay/notify/orderaliPay",
                "/pay/notify/refundaliPay",
                "/productCategory/**",
                "/auth/login",
                "/auth/register",
                "/auth/register/verify",
                "/auth/login/verify",
                "/auth/server/addverify",
                "/product/findProductListByCategoryOrProductIdList",
                "/auth/updatePassword",
                "/auth/updatePassword/verify",
                "/register",
                "/shareRegister",
                "/adminShareRegister",
                "/order/expressSubscriptionCallbackInterface",
                //轮播图
                "/rotationChart/homeRotationChart",
                //轮播图活动
                "/rotationChart/activityAcquisition",
                "/applet/homePage/findRecoProduct",
                "/homePage/findHomePageCategory",
                "/homePage/findHomeProductCategory",
                "/homePage/findRecoProduct",
                "/chlOrder/applist",
                "/ceresProduct/findServerProductByName",
                "/applet/homePage/findCategoryList",
                "/applet/pcProductCategory/findList",
                "/applet/homePage/findServerOrProduct",
                "/applet/homePage/findHomePageCategory",
                "/applet/homePage/findHomeProductCategory",
                "/applet/pcSetting/**",
                "/applet/applet/pcSetting/**",
                "/chlOrder/videoList",
                "/applet/ceresProduct/detail",

                "/adminShareRegisterTest",

                "/im",

                "/memberBaseInfo/selectSameMember",

                "/memberBaseInfo/timeStamp",
                "/auth/shopLogin",
                //记录用户登录
                "/auth/record",
                //晒晒
                "/sun/sun/list",
                //视频
                "/smallVideo/list",
                "/xsySms/receiveMessage",
                "/smallVideo/callback",
                "/info/id/**",
                "/chlOrder/examine"

        };
        return urls;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


}

